﻿Imports System.ComponentModel.Design
Imports System.Runtime.InteropServices

Public Class WCDesigner
    Inherits System.Windows.Forms.Design.ParentControlDesigner
    Private _actionListCollection As DesignerActionListCollection

    ' Enables design-time support (no clue how this works exactly)
    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        If m.Msg = WM_NCHITTEST Then
            If m.Result.ToInt32 = HTTRANSPARENT Then
                m.Result = IntPtr.op_Explicit(HTCLIENT)
            End If
        End If
    End Sub
    Private Structure TCHITTESTINFO
        Public pt As Point
        Public flags As TabControlHitTest
    End Structure
    Private Const WM_NCHITTEST As Integer = &H84
    Private Const HTTRANSPARENT As Integer = -1
    Private Const HTCLIENT As Integer = 1
    Private Const TCM_HITTEST As Int32 = &H130D
    ' Enables design-time support (no clue how this works exactly)
    Protected Overrides Function GetHitTest(ByVal point As System.Drawing.Point) As Boolean

        If (Me.SelectionService.PrimarySelection Is Me.Control) Then
            Dim hti As New TCHITTESTINFO

            hti.pt = Me.Control.PointToClient(point)

            Dim m As New Message
            m.HWnd = Me.Control.Handle
            m.Msg = TCM_HITTEST

            Dim lparam As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(hti))
            Marshal.StructureToPtr(hti, lparam, False)
            m.LParam = lparam

            MyBase.WndProc(m)
            Marshal.FreeHGlobal(lparam)

            If m.Result.ToInt32 <> -1 Then
                Return hti.flags <> TabControlHitTest.TCHT_NOWHERE
            End If

        End If

        Return False

    End Function
    Private Enum TabControlHitTest
        TCHT_NOWHERE = 1
        TCHT_ONITEMICON = 2
        TCHT_ONITEMLABEL = 4
        TCHT_ONITEM = TCHT_ONITEMICON Or TCHT_ONITEMLABEL
    End Enum
    Private m_SelectionService As ISelectionService
    Public ReadOnly Property SelectionService() As ISelectionService
        Get
            If m_SelectionService Is Nothing Then
                m_SelectionService = DirectCast(GetService(GetType(ISelectionService)), ISelectionService)
            End If
            Return m_SelectionService
        End Get
    End Property
End Class
